﻿var app = angular.module("personnelLabel", ['Common', 'cgBusy', 'restangular']);
// 人员悬浮标签
app.directive("labelshowDirective", ["$http", "Restangular", "$state",'$timeout','T','LS','$stateParams','Msg', function ($http, Restangular, $state,$timeout,T,LS,$stateParams,Msg) {
    return {
        scope: {                 
            labelshowShowmi:"@",    // 初次显示的状态     
        },        
        templateUrl: "/system/directive/personnel/personnellable.html",
        transclude: true,
        replace: false,
        controller: function ($scope) {
        },
        controllerAs: "LableCtrl",
        link: function ($scope, elm, attr, LableCtrl) {        
            //console.log(attr);           
            $scope.moreflag = false;
            var timeo;  //创建一个时间
            $scope.showmi = false;
            $scope.showload = true;  
            if($scope.labelshowShowmi == "true"){            
            $timeout(function () {
                $scope.showmi = true;
                $scope.promise = Restangular.one("PersonnelManagement").customGET("PersonnelGetIframeInfo", { user_ID: attr.labelshowDirective }).then(function (data) {
                    if (!data) {
                        console.log("请求数据为空");
                    }
                    $scope.showload = false;
                    $scope.LableObj = data;
                    $timeout(function(){$scope.CalculateWidth()},10);
                    //console.log($scope.LableObj);
                }, function errorCallback() {
                    console.log('请求失败');
                });              
                },300);
            }else{
                    elm.bind("mouseenter", function () {

//                    console.log( attr.title);
                        //console.log(attr.labelshowDirective);
                        //console.log($scope.showmi);                        
                        //保证弹出框中请求一次
                        if (!$scope.showmi) {
                            timeo = $timeout(function () {
                                $scope.promise = Restangular.one("/PersonnelManagement").customGET("PersonnelGetIframeInfo", { user_ID: attr.labelshowDirective }).then(function (data) {
                                    if (!data) {
                                        console.log("请求数据为空");
                                    }
                                    $scope.showload = false;
                                    $scope.LableObj = data; 
                                    var fl = document.getElementById("Character_labelmain");
                                    try{
										if(data.data.logText.length >0 && data.data.userOrgList.length >0 && data.data.userRoleList.length > 0 && data.data.userGroupList.length > 0){
											fl.style.height="90px";                                                    
										}
									}catch(e){}                                                    
                                    //console.log($scope.LableObj);
                                }, function errorCallback() {
                                    console.log('请求失败');
                                });  
                                $scope.$apply(function () {
                                    $scope.showmi = true;
                                });
                            }, 300);
                        }
                        //$scope.CalculateWidth();
                    });
                    elm.bind("mouseleave", function () {
                        //	console.log(elm[0].innerHTML+"出"+attr.labelshowDirective); 
                        $timeout.cancel(timeo);       // 若在规定的时间内移出则删除时间        
                        $scope.$apply(function () {
                            $scope.showmi = false;
                        });
                    });
            }
            // 计算更多的位置
            $scope.CalculateWidth= function(){                
                var fl = document.getElementById("Character_labelmain").offsetWidth- 20 -44;                
                var ul = document.getElementById("Character_labeltitle_first");                 
                //console.log(fl);               
                var lis = ul.getElementsByTagName("li");
                var sum = 0;
                if(lis.length==1){
                ul.style.width = "100%";
                }else{
                    for (var i = 0; i < lis.length; i++) {
                            //console.log(lis[i].offsetWidth);
                            var idwidth =0;
                            if(lis[i].offsetHeight >= 26 ){
                                idwidth = lis[i].offsetHeight/26 * lis[i].offsetWidth;
                            }else{
                                idwidth= lis[i].offsetWidth;
                            }                        
                            if((sum+idwidth) > fl){
                                $scope.moreflag = true;
                                break;
                            }
                            sum= sum+6 +lis[i].offsetWidth;
                    }
                    //console.log(sum);
                    ul.style.width = sum+"px";
                }
            }

        }
    };
} ]);
